package com.youlai.boot.factory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youlai.boot.factory.mapper.ClothingSizesMapper;
import com.youlai.boot.factory.mapper.ColorMapper;
import com.youlai.boot.factory.model.entity.ClothingSizes;
import com.youlai.boot.factory.model.entity.Color;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.factory.mapper.GarmentStyleMapper;
import com.youlai.boot.factory.service.GarmentStyleService;
import com.youlai.boot.factory.model.entity.GarmentStyle;
import com.youlai.boot.factory.model.form.GarmentStyleForm;
import com.youlai.boot.factory.model.query.GarmentStyleQuery;
import com.youlai.boot.factory.model.vo.GarmentStyleVO;
import com.youlai.boot.factory.converter.GarmentStyleConverter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 服装款式服务实现类
 *
 * @author youlaitech
 * @since 2025-07-06 16:02
 */
@Service
@RequiredArgsConstructor
public class GarmentStyleServiceImpl extends ServiceImpl<GarmentStyleMapper, GarmentStyle> implements GarmentStyleService {

    private final GarmentStyleConverter garmentStyleConverter;
    private final GarmentStyleMapper garmentStyleMapper;
    private final ClothingSizesMapper clothingSizesMapper;
    private final ColorMapper colorMapper;

    /**
     * 获取服装款式分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<GarmentStyleVO>} 服装款式分页列表
     */
    @Override
    public IPage<GarmentStyleVO> getGarmentStylePage(GarmentStyleQuery queryParams) {
        Page<GarmentStyleVO> pageVO = this.baseMapper.getGarmentStylePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取服装款式联客户表分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<GarmentStyleVO>} 服装款式分页列表
     */
    @Override
    public IPage<GarmentStyleVO> getGarmentStyleWithCustomer(GarmentStyleQuery queryParams) {
        Page<GarmentStyleVO> pageVO = this.baseMapper.getGarmentStyleWithCustomer(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }


    /**
     * 获取服装款式表单数据
     *
     * @param id 服装款式ID
     * @return 服装款式表单数据
     */
    @Override
    public GarmentStyleForm getGarmentStyleFormData(Long id) {
        GarmentStyle entity = this.getById(id);
        return garmentStyleConverter.toForm(entity);
    }


    /**
     * 获取服装款式表单数据
     *
     * @param id 服装款式ID
     * @return 服装款式表单数据
     */
    @Override
    public GarmentStyleVO getGarmentStyleFormWithCustomer(Long id) {
        GarmentStyleVO entity = garmentStyleMapper.getGarmentStyleWithCustomerById(id);

        // 获取尺寸名称
        if (StrUtil.isNotBlank(entity.getSize())) {
            List<String> sizeIds = Arrays.asList(entity.getSize().split(","));
            List<ClothingSizes> clothingSizes = clothingSizesMapper.selectList(
                    new QueryWrapper<ClothingSizes>().in("id", sizeIds)
            );
            // 提取尺寸名称并组合成字符串
            String sizeNames = clothingSizes.stream()
                    .map(ClothingSizes::getSizeName)
                    .collect(Collectors.joining(","));
            entity.setSizeNames(sizeNames);
        }

        // 获取颜色名称
        if (StrUtil.isNotBlank(entity.getColor())) {
            List<String> colorIds = Arrays.asList(entity.getColor().split(","));
            List<Color> colors = colorMapper.selectList(
                    new QueryWrapper<Color>().in("color_id", colorIds)
            );
            // 提取颜色名称并组合成字符串
            String colorNames = colors.stream()
                    .map(Color::getColorName)
                    .collect(Collectors.joining(","));
            entity.setColorNames(colorNames);
        }

        return entity;
    }


    /**
     * 新增服装款式
     *
     * @param formData 服装款式表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveGarmentStyle(GarmentStyleForm formData) {
        GarmentStyle entity = garmentStyleConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新服装款式
     *
     * @param id       服装款式ID
     * @param formData 服装款式表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateGarmentStyle(Long id, GarmentStyleForm formData) {
        GarmentStyle entity = garmentStyleConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除服装款式
     *
     * @param ids 服装款式ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteGarmentStyles(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的服装款式数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
